﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Cors;
using CVS.DAL;
using CVS.WebAPI.Mappers;
using log4net;

namespace CVS.WebAPI.Controllers
{
    public class TasksController : ApiController
    {
        private static ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        // GET: api/Tasks
        public IHttpActionResult Get()
        {
            try
            {
                var tasks = DataRepository.GetTasks();
                return Ok(TasksMapper.MapTasks(tasks));
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                return InternalServerError(ex);
            }
        }

        // GET: api/Tasks/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/Tasks
        public void Post([FromBody] Task task)
        {
            try
            {
                DataRepository.AddTask(task);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        // PUT: api/Tasks/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/Tasks/5
        public void Delete([FromBody] Task task)
        {
            try
            {
                DataRepository.DeleteTask(task);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
    }
}
